package com.iwhalecloud.bss.kite.cucc.service.attr.handler;

import java.util.List;

import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.service.attr.LocalAttrHelper;
import com.iwhalecloud.bss.kite.cucc.service.util.ProdCateUtil;
import com.iwhalecloud.bss.kite.manager.api.IOperateSceneInstService;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.enums.ActionType;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.attr.handler.AbstractAttrHandler;

import com.ztesoft.bss.common.util.SpringUtil;
import org.springframework.beans.factory.annotation.Autowired;

public class PlatformServicesLevelAttrHandler extends AbstractAttrHandler<KiteInst, AttrData> {
    @Autowired
    private IOperateSceneInstService operateSceneInstService;

    @Override
    public void onShow(KiteAttr attr, KiteInst inst, AttrData data) {
        IQuerySceneInstService querySceneInstService = SpringUtil.getBean(IQuerySceneInstService.class);
        String addRecord = querySceneInstService.getExtParameterValue(data.getSceneInstId(), "taskCode");
        // 物联网补录环节，属性不可编辑
        if (KiteStringUtils.equals(addRecord, "OrderAddRecordWLWXS")) {
            attr.setIsEdit(KeyConsts.IFTRUE_F);
        }
        super.onShow(attr, inst, data);
    }

    @Override
    public void before(KiteAttr attr, KiteInst inst, String value, AttrData data) {
        if (!"100006417".equals(value)) {
            return;
        }
        //100006417 平台高级服务
        if(KiteStringUtils.equals("2826",data.getServiceOfferId())){
            LocalAttrHelper.showAndNonnull(inst, "200004024");
        }
        IQuerySceneInstService querySceneInstService = SpringUtil.getBean(IQuerySceneInstService.class);
        List<KiteProdInst> kiteProdInsts = querySceneInstService.listProdInsts(data.getSceneInstId());
        if (kiteProdInsts.stream().anyMatch(kiteProdInst -> KiteStringUtils
            .isEqual(kiteProdInst.getSpecId(), "100000000015947")
            && !KiteStringUtils.equalsAny(kiteProdInst.getActionType().name(), ActionType.D.name(), ActionType.X.name()))) {
            ErrorConsts.BUSI_COMMON_ERROR.throwOut("附加产品选择是可付费套包，平台服务级别的高级平台不能选择，如需切换成高级，请将可付费套包取消勾选");
        }
    }

    @Override
    public void after(KiteAttr attr, KiteInst inst, AttrData data) {
        super.after(attr, inst, data);
        //产品变更 需置灰
        if (KiteStringUtils.isEqual(data.getServiceOfferId(), "2826")
            && KiteStringUtils.equals(inst.getSpecId(), ProdCateUtil.getMainMemberProdId(ProdCateUtil.GIFT_DZ5G_PROD))) {
            attr.setIsEdit(KeyConsts.IFTRUE_F);
            operateSceneInstService
                .setAttr(data.getSceneInstId(), inst.getInstType(), inst.getInstId(), attr);
        }
    }

}
